import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.UUID;

/**
 Java 8 Base64
     网站：https://www.runoob.com/java/java8-base64.html
 */
public class JDK_8 {
    public static void main(String[] args) {
        try {
            // 使用基本编码
            String base64encodedString = Base64.getEncoder().encodeToString("runoob?java8".getBytes("utf-8"));
            System.out.println("Base64 编码字符串 (基本) :" + base64encodedString); // Base64 编码字符串 (基本) :cnVub29iP2phdmE4
            // 解码
            byte[] base64decodedBytes = Base64.getDecoder().decode(base64encodedString);
            System.out.println("原始字符串: " + new String(base64decodedBytes, "utf-8")); // 原始字符串: runoob?java8
            base64encodedString = Base64.getUrlEncoder().encodeToString("runoob?java8".getBytes("utf-8"));
            System.out.println("Base64 编码字符串 (URL) :" + base64encodedString); // Base64 编码字符串 (URL) :cnVub29iP2phdmE4
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < 10; ++i) {
                stringBuilder.append(UUID.randomUUID().toString());
            }
            byte[] mimeBytes = stringBuilder.toString().getBytes("utf-8");
            String mimeEncodedString = Base64.getMimeEncoder().encodeToString(mimeBytes);
            System.out.println("Base64 编码字符串 (MIME) :" + mimeEncodedString);
            /**
             Base64 编码字符串 (MIME) :
             YWZiMzM3M2YtYTkzZS00NTc3LWI0YTctMTcxYWJmMTNkNzMzMGQ2MmE4ZWMtNTE5Yi00NWM0LTg4
             YjYtY2YzODZkMTg5MGJmZTkwMzRjNTEtYjFhMi00MDA0LTkzNDctMTQxYTUwNGFjN2Y1NzJkZmU0
             MWMtOTA1YS00MDM2LThlZjUtN2RhY2RmNzk3ZmIwNmVlYTE5N2YtMjUyYy00OWM4LWEyZWYtNjVh
             NmQ2ZTJmMjJmZmM5NTcwMjctY2ZiMC00ODQxLTkzMDEtZjYzN2IzZmZiZWNhYmQ4OGI2NzUtODA4
             NS00NzE2LWIwYjEtZWQxYjZmYjBkMWIwMWQyNWYyMjItZTljOS00YjYzLTliZWMtYzMwOTc1MTVj
             ZWIzMGVjOTlmMjEtYzI1ZS00MjY3LWFhZjctYWJmMDY1YmVmNmFlMzkwY2IzMDMtYzFiNC00NTM2
             LTg0ZDAtNDNiY2FmODVhMTdk
             */
        }catch(UnsupportedEncodingException e){
            System.out.println("Error :" + e.getMessage());
        }
    }
}